package com.cn.JUC.utils.case1;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Title TODO
 * @Description TODO
 * @Author CAI ZHAI
 * @Date 2021/8/31 22:01
 **/
@Slf4j
public class LockConditiong{
    
    private  static final Lock rtl = new ReentrantLock();
    public static int value = 0;
    
    /* @Point Lock
     * @Analyse  既然synchronized已经实现了管程，为什么还要重复造轮子？
     *           因为Lock能够响应中断、支持超时、非阻塞地获取锁
     * @Date     2021/8/30 22:02
     **/
    public static void addOne(){    //获取锁
        //rtl.lock();   阻塞获取锁
        boolean b = false;     //非阻塞获取锁
        try{
            b = rtl.tryLock(1, TimeUnit.SECONDS);
    
            if(b){
                value += 1;
            }
        }catch(InterruptedException e){
            e.printStackTrace();
        }finally{
            rtl.unlock();
        }
        
    }
}
